package com.jjlin.chapter_2;
import java.util.*;

public class CD113{
    public static class Node{
        public int val;
        public Node next;
        public Node(int data){
            this.val = data;
        }
    }

    public static Node createNodeList(int[] nums){
        Node head = new Node(nums[0]);
        Node tmp = head;
        for(int i = 1; i < nums.length; i++){
            Node newNode = new Node(nums[i]);
            tmp.next = newNode;
            tmp = newNode;
        }
        return head;
    }

    public static Node listPartition(Node head, int pivot){
        if(head == null)
            return head;
        //统计链表长度
        int i = 0;
        Node cur = head;
        while(cur != null){
            i++;
            cur = cur.next;
        }
        //创建数组并填充
        Node[] nodeArr = new Node[i];
        cur = head;
        for(i = 0; i < nodeArr.length; i++){
            nodeArr[i] = cur;
            cur = cur.next;
        }
        arrPartition(nodeArr, pivot);
        for(i = 1; i < nodeArr.length; i++)
            nodeArr[i - 1].next = nodeArr[i];
        nodeArr[i - 1].next = null;//注意要将最后一个节点的next指针置空，要不然会变成循环链表
        return nodeArr[0];
    }

    //进阶解法：空间复杂度为O(1)，同时保证稳定性
    public static Node listPartition1(Node head, int pivot){
        if(head == null)
            return head;
        Node sH = null; //小的头
        Node sT = null; //小的尾
        Node eH = null; //等于的头
        Node eT = null; //等于的尾
        Node bH = null; //大的头
        Node bT = null; //大的尾
        Node next = null; //保存下一节点
        while(head != null){
            next = head.next;
            head.next = null;
            if(head.val < pivot){
                if(sH == null){
                    sH = head;
                    sT = head;
                }else{
                    sT.next = head;
                    sT = head;
                }
            }else if(head.val == pivot){
                if(eH == null){
                    eH = head;
                    eT = head;
                }else{
                    eT.next = head;
                    eT = head;
                }
            }else{
                if(bH == null){
                    bH = head;
                    bT = head;
                }else{
                    bT.next = head;
                    bT = head;
                }
            }
            head = next;
        }
        if(sH != null){ //小和等于链表相连
            sT.next = eH;
            eT = eT == null ? sT : eT;
        }
        if(eT != null)//将大链表相连
            eT.next = bH;
        return sH != null ? sH : eH != null ? eH : bH;
    }

    //划分函数
    public static void arrPartition(Node[] nodeArr, int pivot){
        int less = -1;
        int more = nodeArr.length;
        int cur = 0;
        while(cur < more){
            if(nodeArr[cur].val < pivot)
                swap(nodeArr, ++less, cur++);
            else if(nodeArr[cur].val > pivot)
                swap(nodeArr, --more, cur);
            else
                cur++;
        }
    }

    public static void swap(Node[] arr, int i, int j){
        Node tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void printNodeList(Node head){
        Node cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int pivot = sc.nextInt();
        int[] nums = new int[n];
        for(int i = 0; i < n; i++)
            nums[i] = sc.nextInt();
        Node head = createNodeList(nums);
        head = listPartition(head, pivot);
        printNodeList(head);
    }
}

